#!/usr/bin/env python

#    FlyBack
#    Copyright (C) 2007 Derek Anderson
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import desktop
import help_data
import config_backend
import backup_backend
import main_gui
import getopt, sys


def print_help():
    print (
        """ Flyback - Usage
        -b
        --backup
        --backup-all
            Don't start the GUI, excecute all the backup schemes now.

        --backup-scheme=<scheme-id>
            Execute just one backup scheme now.

        --config-file=<file>
            Use the specified config file to read and store the configuration
            instead of gconf.
            If this option is omitted, gconf will be used.
            
        --help
            print this screen.
        """)






def main():
    # parse command line options
    try:
        opts, args = getopt.getopt(sys.argv[1:], ["h","b"], ["help","backup","backup-all", "backup_scheme=","config-file="])
    except getopt.error, msg:
        print msg
        print_help()
        sys.exit(2)

    # TODO - Need to process config backend arguments
    
    
    # process options
    perform_backup=False
    backup_scheme=None
    use_config_file=False
    config_file=None
    
    for o, a in opts:
        if o in ("-h", "--help"):
            print_help()
            sys.exit(0)
            
        elif o in ("-b", "--backup", "--backup-all"):
            perform_backup=True
            backup_scheme=1
            
        elif o == "--backup_scheme=":
            backup_scheme=a
            perform_backup=True
            
        elif o == "--config-file=":
            config_file=a
            use_config_file=True
            
    if use_config_file:
        config = config_backend.Config(config_backend.BackupSchemeConfig.FILE, config_file)
            
    else:
        config = config_backend.Config()
        
    
    if perform_backup:
        # Execute just one backup
        if backup_scheme:
            backup_scheme_config = config.get_backup_scheme_config(backup_scheme)
            backup_backend.Backup().backup(backup_scheme_config)
            
        # Execute all backup schemes
        else:
            for scheme_id in config.get_backup_schemes():
                backup_scheme_config = config.get_backup_scheme_config(scheme_id)
                backup_backend.Backup().backup(backup_scheme_config)
            
            
        sys.exit(0)
            

    main_gui.MainGUI(config)


if __name__ == "__main__":
    main()
        
